package top.codedance.iotp.tools.serverTool.ui;

import com.alibaba.fastjson.JSONObject;
import top.codedance.iotp.iot.sdk.StandandService;
import top.codedance.iotp.iot.sdk.po.sync.BaseParam;
import top.codedance.iotp.tools.serverTool.ServerToolApplication;
import top.codedance.iotp.tools.terminal.TerminalApplication;
import top.codedance.iotp.tools.terminal.ui.WaitLoading;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.net.URL;
import java.util.Map;
import java.util.Set;

/**
 * @author Lenovo
 */
public class ServerInfoFrame extends javax.swing.JDialog {
    private WaitLoading waitLoading = new WaitLoading(this);
    private MainFrame mf;
    private StandandService standandService;
    private BaseParam baseParam;
    private DefaultTableModel dtm;

    /**
     * Creates new form ServerInfoFrame
     */
    public ServerInfoFrame(MainFrame mf) {
        super(mf, true);
        this.mf = mf;
        initComponents();
        URL url = Thread.currentThread().getContextClassLoader().getResource("logo.gif");
        ImageIcon icon = new ImageIcon(url);
        setIconImage(icon.getImage());
        setMinimumSize(new Dimension(1062, 643));
        setLocationRelativeTo(this.mf.getShellFrame());
        setTitle("代码飞舞采控系统-查看服务器状态");
        this.standandService = new StandandService(ServerToolApplication.serverConf.getServerHost());
        this.baseParam = new BaseParam();
        baseParam.setUsername(ServerToolApplication.serverConf.getUsername());
        baseParam.setPassword(ServerToolApplication.serverConf.getPassword());

        initTable();
    }

    private void initTable() {
        String[] columnNames = {"key", "value"};
        Object[][] tdata = new Object[0][0];
        dtm = new DefaultTableModel(tdata, columnNames);
        result_jt.setModel(dtm);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        button1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        result_jt = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        button1.setLabel("查看");
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button1ActionPerformed(evt);
            }
        });

        result_jt.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                        {},
                        {},
                        {},
                        {}
                },
                new String[]{

                }
        ));
        result_jt.setToolTipText("");
        result_jt.setRowHeight(30);
        jScrollPane2.setViewportView(result_jt);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(599, Short.MAX_VALUE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                        .addContainerGap()
                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 629, Short.MAX_VALUE)
                                        .addContainerGap()))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(412, Short.MAX_VALUE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGap(47, 47, 47)
                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE)
                                        .addContainerGap()))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button1ActionPerformed
        waitLoading.open();
        TerminalApplication.pool.submit(() -> {
            try {
                JSONObject jsonObject = standandService.serverInfomation(baseParam);
                Integer status = jsonObject.getInteger("status");
                if (status == 200) {
                    initTable();
                    JSONObject data = jsonObject.getJSONObject("data");
                    Set<Map.Entry<String, Object>> entrySet = data.entrySet();
                    int i = 0;
                    for (Map.Entry<String, Object> item : entrySet) {
                        dtm.addRow(new Object[]{item.getKey(), item.getValue().toString()});
                        i++;
                    }
                } else {
                    JOptionPane.showMessageDialog(rootPane, "请求失败：" + jsonObject.getString("message"));
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(rootPane, "更新失败");
            } finally {
                waitLoading.close();
            }
        });

    }//GEN-LAST:event_button1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton button1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable result_jt;
    // End of variables declaration//GEN-END:variables
}
